package com.kingint.common.unitedresult;

import com.kingint.common.unitedresult.Result.ResultCode;
import com.kingint.common.unitedresult.Result.ResultMessage;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 统一返回类
 */
@Data
public class K {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Object data;

    private K(){}

    public static K ok(){
        K k = new K();
        k.setSuccess(true);
        k.setCode(ResultCode.SUCCESS);
        k.setMessage(ResultMessage.SUCCESS);
        return k;
    }

    public static K error(){
        K k = new K();
        k.setSuccess(false);
        k.setCode(ResultCode.FAILED);
        k.setMessage(ResultMessage.ERROR);
        return k;
    }

    /**
     * 没有权限
     * @return
     */
    public static K forbidden(){
        K k = new K();
        k.setSuccess(false);
        k.setCode(ResultCode.FORBIDDEN);
        k.setMessage(ResultMessage.ERROR);
        return k;
    }

    /**
     * 未登陆或者登陆失效
     * @return
     */
    public static K unauthorized(){
        K k = new K();
        k.setSuccess(false);
        k.setCode(ResultCode.UNAUTHORIZED);
        k.setMessage(ResultMessage.ERROR);
        return k;
    }
    public K success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public K message(String message){
        this.setMessage(message);
        return this;
    }

    public K code(Integer code){
        this.setCode(code);
        return this;
    }

    public K data(Object obj){
        this.setData(obj);
        return this;
    }

}
